#include "net_process.h"
#include "string.h"
#include "esp_log.h"
#include "esp_err.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "mp3.h"
#include "periph_wifi.h"
#include "httpapi.h"
#include "mywifi.h"

#define TAG "net_process"
#define NET_GET_SONG_LIST   0x01
#define NET_GET_SONG_INFO   0x02
#define NET_GET_PLAY_INFO   0x04
#define NET_GET_PLAY_LYRIC  0x08


void (*get_songlist)(SongNode *p);
void (*get_songurl)(music_info *info, char *p);
void (*get_songlyric)(music_info *info, char *p);
void (*get_songurl_before)(music_info *info);

static int indexNow;
static SongNode *songsHead = 0;
static SongNode *songNow;
static char *songListId;
static char *playUrl;
static char *playLyric;

EventGroupHandle_t netEvent = NULL;

void net_process_run(void *p)
{
    // esp_periph_set_handle_t set = (esp_periph_set_handle_t)p;

    // periph_wifi_cfg_t wifi_cfg =
    // {
    //     .wifi_config.sta.ssid = "miio_default",
    //     .wifi_config.sta.password = "123456789",
    // };
    // esp_periph_handle_t wifi_handle = periph_wifi_init(&wifi_cfg);
    // esp_periph_start(set, wifi_handle);

    // periph_wifi_wait_for_connected(wifi_handle, portMAX_DELAY);
    mp3_wait_init();

    while (1)
    {
        EventBits_t eventRet = xEventGroupWaitBits(netEvent, 0xffff, pdTRUE, pdFALSE, portMAX_DELAY);
        if (eventRet & NET_GET_SONG_LIST)
        {
            if (my_wifi_is_connected() == false)
            {
                continue;
            }
            if (net_get_songlist(songListId, &songsHead) == ESP_OK)
            {
                songNow = songsHead;
                if (get_songlist != NULL)
                {
                    get_songlist(songsHead);
                }
            }
            else
            {
                vTaskDelay(pdMS_TO_TICKS(1000));
                xEventGroupSetBits(netEvent, NET_GET_SONG_LIST);
            }

        }
        if (eventRet & NET_GET_SONG_INFO)
        {

        }
        if (eventRet & NET_GET_PLAY_INFO)
        {
            if (playUrl != 0)
            {
                free(playUrl);
                playUrl = 0;
            }
            if (songNow != NULL)
            {
                if (get_songurl_before != NULL)
                {
                    get_songurl_before(songNow->music);
                }
                net_get_music_url(songNow->music, &playUrl);
                if (get_songurl != NULL)
                {
                    get_songurl(songNow->music, playUrl);
                }
            }
        }
        if (eventRet & NET_GET_PLAY_LYRIC)
        {
            if (playLyric != 0)
            {
                free(playLyric);
                playLyric = 0;
            }
            if (songNow != NULL)
            {
                net_get_music_lyric(songNow->music, &playLyric);
                if (get_songlyric != NULL)
                {
                    get_songlyric(songNow->music, playLyric);
                }
            }
        }

    }
}

void net_process_init(esp_periph_set_handle_t set)
{
    netEvent = xEventGroupCreate();
    indexNow = 0;
    xTaskCreate(net_process_run, "net_process", 4096, set, 0, NULL);
}

void net_songlist(char *id, void (*cb)(SongNode *p))
{
    //设置回调函数
    if (songListId != 0)
    {
        free(songListId);
        songListId = 0;
    }
    indexNow = 0;
    get_songlist = cb;
    int len = strlen(id);
    songListId = (char *)malloc(len + 1);
    strcpy(songListId, id);
    songListId[len] = 0;
    //设置事件标志
    xEventGroupSetBits(netEvent, NET_GET_SONG_LIST);
}

/**
 * @brief
 *
 * @param index
 * @param cb
 * @param user
 * @return char 0:ok 1:空头 2:超出
 */
char net_choose_song(int index, void(*before_cb)(music_info *info), void (*cb)(music_info *info, char *p))
{
    if (songsHead == 0)
    {
        ESP_LOGE(TAG, "songsHead is null");
        return 1;
    }

    SongNode *p = songsHead;
    int i = 0;
    do
    {
        if (i == index)
        {
            songNow = p;
            ESP_LOGI(TAG, "choose song %s", songNow->music->name);
            break;
        }
        else
        {
            p = p->next;
            i++;
        }
    }
    while (p != songsHead);
    if (p == songsHead && i != 0)
    {
        ESP_LOGE(TAG, "index %d is out of range", index);
        return 2;
    }
    get_songurl = cb;
    get_songurl_before = before_cb;
    indexNow = index;
    xEventGroupSetBits(netEvent, NET_GET_PLAY_INFO);
    return 0;
}

int net_get_song_now(music_info **p)
{
    if (p != 0)
    {
        *p = songNow->music;
    }

    int i = 0;
    SongNode *node = songsHead;
    do
    {
        if (songNow == node)
        {
            ESP_LOGI(TAG, "%d,%s", i, songNow->music->name);
            return i;
        }
        else
        {
            node = node->next;
            i++;
        }
    }
    while (node != songsHead);
    return -1;
}
void net_next_song(void(*before_cb)(music_info *info), void (*cb)(music_info *info, char *p))
{
    songNow = song_list_next(songNow);
    get_songurl = cb;
    get_songurl_before = before_cb;
    xEventGroupSetBits(netEvent, NET_GET_PLAY_INFO);
}
void net_prev_song(void(*before_cb)(music_info *info), void (*cb)(music_info *info, char *p))
{
    songNow = song_list_prev(songNow);
    get_songurl = cb;
    get_songurl_before = before_cb;
    xEventGroupSetBits(netEvent, NET_GET_PLAY_INFO);
}

void net_get_lyric(void (*cb)(music_info *info, char *p))
{
    get_songlyric = cb;
    xEventGroupSetBits(netEvent, NET_GET_PLAY_LYRIC);
}


SongNode *net_song_now()
{
    return songNow;
}

void net_song_retry(void(*before_cb)(music_info *info), void (*cb)(music_info *info, char *p))
{
    get_songurl = cb;
    get_songurl_before = before_cb;
    xEventGroupSetBits(netEvent, NET_GET_PLAY_INFO);
}